package com.zhuss.order.processor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.zhuss.bean.vo.core.DetailImportVo;
import com.zhuss.bean.vo.order.order.ListOrderVo;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.ExcelUtil;
import com.zhuss.util.NoUtil;

@Component
public class OrderExport {
	
	@Value("${filePath}")
    private String filePath;
	
	@Value("${fileReadPath}")
	private String fileReadPath;
	
	public Object process(List<ListOrderVo> orderList) throws DzcxException {
		transfer(orderList);
		if (orderList.isEmpty()) {
			orderList.add(new ListOrderVo());
		}
		String url = "";
		String path = filePath +  "batch\\order\\";
		String readPath = fileReadPath +  "batch\\order\\";
		String fileName = "";
		fileName = NoUtil.gen();
		File file2 = new File(path);
		if (!file2.exists()) {
			file2.mkdirs();
		}
		LinkedHashMap<String,String> map = ExcelUtil.getExportOrderMap();
		File file3 = new File(path + "订单导出-" + fileName + ".xls");
		OutputStream os = null;
		try {
			os = new FileOutputStream(file3);
			ExcelUtil.listToExcel(orderList, map, "订单导出", os);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DzcxException(ExceptionCode.导出失败);
		}
		
		url = readPath + file3.getName();
		
		DetailImportVo result = new DetailImportVo();
		result.setUrl(url);
		return result;
	}

	private void transfer(List<ListOrderVo> orderList) {
		for (ListOrderVo vo : orderList) {
			StringBuffer sb = new StringBuffer();
			sb.append("[");
/*			for (OrderItem pi : vo.getOrderList()) {
				String name = pi.getOrderName() + " X " + pi.getCount();
				sb.append(name);
				sb.append(",");
			}
*/			int i = sb.lastIndexOf(",");
			if (i != -1) {
				sb.deleteCharAt(i);
			}
			sb.append("]");
			/*vo.setOrderListName(sb.toString());
			vo.setTypeName(vo.getTypeName());
			vo.setAmountName(vo.getAmountName());
			vo.setStatusName(vo.getStatusName());
			vo.setCreateTimeName(vo.getCreateTimeName());*/
		}
		
	}

}
